// Draggable functionality for DOM elements
// Source: github.com/NathanRutzky/jQuery.dragmove
// Version: 1.0
define(function (require, exports, module) {  
    
(function($) {

    $.fn.dragmove = function() {
    
        return this.each(function() {   
           
            var $parent = $(this).offsetParent(),
                $this = $(this),
                active,
                startX,
                startY;          
            
            $this.on('mousedown touchstart', function(e) {    
                
                active = true;
                startX = e.originalEvent.pageX - $this.offset().left;
                startY = e.originalEvent.pageY - $this.offset().top;               
                                   
                click = $this;                             
                                    
                $parent.on('mousemove touchmove', startMove);    
                
                $this.on('mouseup touchend', function() {          
                    $parent.off('mousemove touchmove', startMove)
                    active = false;                
                });	
                
                return true;   
            });
            
            
            function startMove(e) {                 
                if (e.which != 1) active = false;           
                
                if (active && !$this[0].resize) {    
                    var ptOld = click.offset();
                    click.offset({                     
                        left: e.originalEvent.pageX - startX,
                        top: e.originalEvent.pageY - startY                     
                    });      
                                        
                    var ptNew = click.offset();
                    var ptParent = $parent.offset();
                    
                    if(ptNew.left < ptParent.left || ptNew.left > ptParent.left + $parent.width() - click.width())
                        click.offset({left: ptOld.left});     
                       
                    if (ptNew.top < ptParent.top || ptNew.top > ptParent.top + $parent.height() - click.height())
                         click.offset({top: ptOld.top});     
                }     
            };
            
           	
                                
        });
            
    };

})(jQuery);
})